package pers.qianyu.month_202012.date_20201210;

import org.junit.*;

import java.util.*;

/**
 * [巧妙]
 * 78. 子集
 * https://leetcode-cn.com/problems/subsets/
 *
 * @author mizzle rain
 * @date 2020-12-10 17:07
 */
public class Subsets2 {
    /**
     * 回溯实现
     */
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null || nums.length == 0) {
            return res;
        }
        ArrayList<Integer> list = new ArrayList<>();
        recur(list, nums, 0);
        return res;
    }

    private void recur(List<Integer> list, int[] nums, int start) {
        res.add(new ArrayList<>(list));
        for (int k = start; k < nums.length; k++) {
            list.add(nums[k]);
            recur(list, nums, k + 1);
            list.remove(Integer.valueOf(nums[k]));
        }
    }

    @Test
    public void test1() {
        int[] arr = {1, 2, 3};
        List<List<Integer>> result = new Subsets2().subsets(arr);
        result.forEach(System.out::println);
    }
}
